package com.rural.bus.driver.adapter;

import java.util.List;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.rural.bus.driver.R;
import com.rural.bus.driver.adapter.base.MyBaseAdapter;
import com.rural.bus.driver.model.Order;
import com.rural.bus.driver.utils.DateUtil;

public class NewOrderAdapter extends MyBaseAdapter<Order> {

	private List<Order> mOrders;
	private Context mContext;
	private ListView listView;
	private boolean isNeedTriggerTimer = true;

	public NewOrderAdapter(Context context, List<Order> orders, ListView listview) {
		super(context, orders);
		this.mOrders = orders;
		this.mContext = context;
		this.listView = listview;
		startTimerDown();
	}

	@Override
	public int getCount() {
		return mOrders.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.item_new_order_list, null);

			holder.tv_take_time = (TextView) convertView.findViewById(R.id.tv_take_time);
			holder.tv_up_address = (TextView) convertView.findViewById(R.id.tv_up_address);
			holder.tv_down_address = (TextView) convertView.findViewById(R.id.tv_down_address);
			holder.tv_count_down = (TextView) convertView.findViewById(R.id.tv_count_down);

			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();
		}

		Order order = mOrders.get(arg0);

		holder.tv_take_time.setText("" + DateUtil.formatDiscriptionTime(order.takeTime));
		holder.tv_up_address.setText("" + order.startLocation);
		holder.tv_down_address.setText("" + order.endLocation);
		holder.tv_count_down.setText("" + order.expireTime + "s");

		return convertView;
	}

	public class ViewHolder {
		TextView tv_take_time;
		TextView tv_up_address;
		TextView tv_down_address;
		TextView tv_count_down;
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				notifyDataSetChanged();
				break;
			}
		}

	};

	private void startTimerDown() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					for (int index = 0; index < mOrders.size(); index++) {
						Order order = mOrders.get(index);
						int time = order.expireTime;
						if (time > 1) {
							order.expireTime--;
							Log.d("bus", ""+order.expireTime);
						} else {
							mOrders.remove(order);
						}
						handler.sendEmptyMessage(1);
					}
					try {
						Thread.sleep(1000);
					} catch (Exception e) {

					}
				}

			}
		}).start();
	}
}
